--[[
* MapNotesGathererPlugin for MapLib 2.0
* File: MapNotesGathererPlugin\MapNotesGathererPlugin.lua
* $Revision: 151 $
* $Date: 2007-01-24 14:29:01 +0000 (Wed, 24 Jan 2007) $
* 
* Based on GathererMapNotes.lua Copyright (c) GathererTeam (www.gathereraddon.com)
* Copyright (c) 2007, Mery (Moritz Metelmann)
* All rights reserved.
* 
*	License:
*	This program is free software; you can redistribute it and/or
*	modify it under the terms of the GNU General Public License
*	as published by the Free Software Foundation; either version 2
*	of the License, or (at your option) any later version.
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*	GNU General Public License for more details.
*
*	You should have received a copy of the GNU General Public License
*	along with this program(see GPL.txt); if not, write to the Free Software
*	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
*	Note:
*		This AddOn's source code is specifically designed to work with
*		World of Warcraft's interpreted AddOn system.
*		You have an implicit licence to use this AddOn with these facilities
*		since that is it's designated purpose as per:
*		http://www.fsf.org/licensing/licenses/gpl-faq.html#InterpreterIncompat
*
* Personal Note by Mery: I don't know much about licensing, so if I did something wrong, please let me know.
]]--

MapNotesGathererPlugin = MapNotesGathererPlugin or {};
MapNotesGathererPlugin.Icons = {};

--~ MapNotesInstance.INSTANCE_NORMAL = { file = MapLib.PATH.."\\Icons\\INSTANCE_NORMAL", size = 20 };
--~ MapNotesInstance.INSTANCE_BG = { file = MapLib.PATH.."\\Icons\\INSTANCE_BG", size = 20 };
--~ MapNotesInstance.INSTANCE_LOCKOUT = { file = MapLib.PATH.."\\Icons\\INSTANCE_LOCKOUT", size = 20 };
--~ MapNotesInstance.INSTANCE_HEROIC = { file = MapLib.PATH.."\\Icons\\INSTANCE_HEROIC", size = 20 };
--~ MapNotesInstance.INSTANCE_WHITE = { file = "Interface\\AddOns\\MapNotesInstance\\Icons\\INSTANCE_WHITE", size = 20 };

local L = MapNotesGathererPlugin.Localization;
--~ local L_ML = MapLib.Localization;

-- Generate the icon textures for the Gatherer categories:
local submenu = {};
submenu.HER = {};
submenu.ORE = {};
submenu.TRE = {};
for nodeID, categoryID in pairs(Gatherer.Categories.ObjectCategories) do
	MapNotesGathererPlugin.Icons[nodeID] = {};
	local icon = MapNotesGathererPlugin.Icons[nodeID];
	icon.file = Gatherer.Util.GetNodeTexture(nodeID);
	icon.size = 16;
	local name;
	for k, v in pairs(Gatherer.Nodes.Names) do
		if (nodeID == v) then
			name = k;
		end;
	end;
	local submenuEntry = {text = name, texture = icon, style = nodeID, default = true}
	tinsert(submenu[strsub(categoryID, 1, 3)], submenuEntry);
end;

local function submenusort(table1, table2)
	if (not table1.text or not table2.text) then return; end;
	if (table1.text < table2.text) then
		return true;
	else
		return false;
	end;
end;

table.sort(submenu.HER, submenusort);
table.sort(submenu.ORE, submenusort);
table.sort(submenu.TRE, submenusort);
submenusort = nil;

--Create POI Tooltip
local fr = CreateFrame("Frame", "MapNotesGathererPluginTooltip", MapLibFrame);
fr:Hide();
fr:SetFrameStrata("TOOLTIP");
fr:SetBackdrop({
	bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", 
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", tile = true, tileSize = 16, edgeSize = 16, 
	insets = { left = 5, right = 5, top = 5, bottom = 5 }
});
fr:SetBackdropBorderColor(MapLib.colors[3].r, MapLib.colors[3].g, MapLib.colors[3].b);
fr:SetBackdropColor(MapLib.colors[4].r, MapLib.colors[4].g, MapLib.colors[4].b);
fr:SetWidth(180);
fr:SetHeight(41); -- 25 = one line, 41 = 2 lines
fr:SetPoint("CENTER", MapLibButton, "CENTER", 0, 0);
fr:SetFrameLevel(MapLib.FRAMELEVEL_MENU_AND_TOOLTIP);

local texture = fr:CreateTexture("MapNotesGathererPluginTooltipBG", "ARTWORK");
texture:SetHeight(16);
texture:SetWidth(170);
texture:SetTexture(MapLib.PATH.."\\Artwork\\BG");
texture:SetPoint("TOP", MapNotesGathererPluginTooltip, "TOP", 0, -5);
texture:SetVertexColor(MapLib.colors[1].r, MapLib.colors[1].g, MapLib.colors[1].b);

texture = fr:CreateTexture("MapNotesGathererPluginTooltipIcon", "OVERLAY");
texture:SetHeight(14);
texture:SetWidth(14);
texture:SetPoint("LEFT", MapNotesGathererPluginTooltipBG, "LEFT", 2, 0);

local fontstring = fr:CreateFontString("MapNotesGathererPluginTooltipHeader", "OVERLAY", "GameFontHighlightSmall");
fontstring:SetText("");
fontstring:SetJustifyH("LEFT");
fontstring:SetHeight(16);
fontstring:SetWidth(150);
fontstring:SetPoint("LEFT", MapNotesGathererPluginTooltipIcon, "RIGHT", 0, 1);

fontstring = fr:CreateFontString("MapNotesGathererPluginTooltipCount", "OVERLAY", "GameFontHighlightSmall");
fontstring:SetText("");
fontstring:SetJustifyH("LEFT");
fontstring:SetHeight(16);
fontstring:SetWidth(170);
fontstring:SetPoint("TOP", MapNotesGathererPluginTooltipBG, "BOTTOM", 0, 2);	
fontstring = fr:CreateFontString("MapNotesGathererPluginTooltipWho", "OVERLAY", "GameFontHighlightSmall");
fontstring:SetText("");
fontstring:SetJustifyH("LEFT");
fontstring:SetHeight(16);
fontstring:SetWidth(170);
fontstring:SetPoint("TOP", MapNotesGathererPluginTooltipCount, "BOTTOM", 0, 2);
fontstring = fr:CreateFontString("MapNotesGathererPluginTooltipLast", "OVERLAY", "GameFontHighlightSmall");
fontstring:SetText("");
fontstring:SetJustifyH("LEFT");
fontstring:SetHeight(16);
fontstring:SetWidth(170);
fontstring:SetPoint("TOP", MapNotesGathererPluginTooltipWho, "BOTTOM", 0, 2);

local itemlines = 0;
MapNotesGathererPlugin.CreateItemLine = function()
	itemlines = itemlines + 1;
	local texture = MapNotesGathererPluginTooltip:CreateTexture("MapNotesGathererPluginTooltipItemTexture"..itemlines, "OVERLAY");
	texture:SetWidth(10);
	texture:SetHeight(10);
	local fontstring = MapNotesGathererPluginTooltip:CreateFontString("MapNotesGathererPluginTooltipItemLeftText"..itemlines, "OVERLAY", "GameFontHighlightSmall");
	fontstring:SetText("");
	fontstring:SetJustifyH("LEFT");
	fontstring:SetHeight(16);
	fontstring:SetWidth(122);
	fontstring:SetPoint("LEFT", texture, "RIGHT", 2, 0);
	local fontstring2 = MapNotesGathererPluginTooltip:CreateFontString("MapNotesGathererPluginTooltipItemRightText"..itemlines, "OVERLAY", "GameFontNormalSmall");
	fontstring2:SetText("");
	fontstring2:SetJustifyH("RIGHT");
	fontstring2:SetHeight(16);
	fontstring2:SetWidth(134);
	fontstring2:SetPoint("RIGHT", fontstring, "RIGHT", 32, 0);
	if (itemlines == 1) then
		texture:SetPoint("TOPLEFT", MapNotesGathererPluginTooltipLast, "BOTTOMLEFT", 1, -1);
	else
		texture:SetPoint("TOPLEFT", "MapNotesGathererPluginTooltipItemTexture"..itemlines-1, "BOTTOMLEFT", 0, -4);
	end;
end;


MapNotesGathererPlugin.GetNumNodes = function()
	local continent = GetCurrentMapContinent();
	local zone = GetCurrentMapZone();
	local count = 0;
	if ( Gatherer.Storage.HasDataOnZone(continent, zone) ) then
		for nodeID, gatherType, num in Gatherer.Storage.ZoneGatherNames(continent, zone) do
			count = count + num;
		end;
	end;
--~ 	DevTools_Dump(count);
	return count;
end;

MapNotesGathererPlugin.GetNode = function(index)
	local continent = GetCurrentMapContinent();
	local zone = GetCurrentMapZone();
	local count = 0;
	for nodeID, gatherType, num in Gatherer.Storage.ZoneGatherNames(continent, zone) do
		if (count + num >= index) then
			index = index-count;
			local xPos, yPos = Gatherer.Storage.GetNodeInfo(continent, zone, nodeID, index);
			local name = Gatherer.Util.GetNodeName(nodeID);
			return index, name, MapNotesGathererPlugin.Icons[nodeID], xPos, yPos, nodeID, 1;
		else
			count = count + num;
		end;
	end;
end;

local _tr = Gatherer.Locale.Tr;
MapNotesGathererPlugin.GetTooltip = function(id, title)
	local continent = GetCurrentMapContinent();
	local zone = GetCurrentMapZone();
	local nodeID;
	-- find the nodeID by the name
	for name, nID in pairs(Gatherer.Nodes.Names) do
		if (name == title) then
			nodeID = nID;
			break;
		end;
	end;
	local _, _, count, gType, harvested, inspected, who = Gatherer.Storage.GetNodeInfo(continent, zone, nodeID, id);
	
	MapNotesGathererPluginTooltipHeader:SetText(title);
	if (MapNotesGathererPlugin.Icons[nodeID].file) then 
		MapNotesGathererPluginTooltipIcon:SetTexture(MapNotesGathererPlugin.Icons[nodeID].file);
		MapNotesGathererPluginTooltipIcon:Show();
	else
		MapNotesGathererPluginTooltipIcon:Hide();
	end;
	
	local last = inspected or harvested
	local height = 25;
	if (count and count > 0) then
		MapNotesGathererPluginTooltipCount:SetText(_tr("NOTE_COUNT", count));
		MapNotesGathererPluginTooltipCount:SetPoint("TOP", MapNotesGathererPluginTooltipBG, "BOTTOM", 0, 2);
		height = height + 14;
	else
		MapNotesGathererPluginTooltipCount:SetText("");
		MapNotesGathererPluginTooltipCount:SetPoint("TOP", MapNotesGathererPluginTooltipBG, "BOTTOM", 0, 18);	end
	if (who) then
		if (who == "REQUIRE") then
			MapNotesGathererPluginTooltipWho:SetText(_tr("NOTE_UNSKILLED"))
		elseif (who == "IMPORTED") then
			MapNotesGathererPluginTooltipWho:SetText(_tr("NOTE_IMPORTED"))
		else
			MapNotesGathererPluginTooltipWho:SetText(_tr("NOTE_SOURCE", who))
		end
		MapNotesGathererPluginTooltipWho:SetPoint("TOP", MapNotesGathererPluginTooltipCount, "BOTTOM", 0, 2);
		height = height + 14;
	else
		MapNotesGathererPluginTooltipWho:SetText("");
		MapNotesGathererPluginTooltipWho:SetPoint("TOP", MapNotesGathererPluginTooltipCount, "BOTTOM", 0, 18);
	end
	if (last and last > 0) then
		MapNotesGathererPluginTooltipLast:SetText(_tr("NOTE_LASTSEEN", format("%d", time()-last)));
		MapNotesGathererPluginTooltipLast:SetPoint("TOP", MapNotesGathererPluginTooltipWho, "BOTTOM", 0, 2);
		height = height + 14;
	else
		MapNotesGathererPluginTooltipLast:SetText("");
		MapNotesGathererPluginTooltipLast:SetPoint("TOP", MapNotesGathererPluginTooltipWho, "BOTTOM", 0, 18);
	end
	
	local itemlinecount = 0;
	if (GatherDrops) then
		local drops = GatherDrops[nodeID]
		if (drops) then
			local total = drops.total
			if (total and total > 0) then
--~ 				tooltip:AddLine(_tr("Overall drop rates:"))
				for item, count in pairs(drops) do
					if (item ~= "total") then
						local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, itemSubType, itemStackCount, itemEquipLoc, invTexture = GetItemInfo(item) 
						
						if (itemName) then
							local expected = count/total
							if (expected > 0.01) then
								itemlinecount = itemlinecount + 1;
								if (itemlinecount > itemlines) then MapNotesGathererPlugin.CreateItemLine(); end;
								getglobal("MapNotesGathererPluginTooltipItemTexture"..itemlinecount):SetTexture(invTexture);
								getglobal("MapNotesGathererPluginTooltipItemLeftText"..itemlinecount):SetText(itemName);
								getglobal("MapNotesGathererPluginTooltipItemRightText"..itemlinecount):SetText(string.format("x%0.2f", expected));
								getglobal("MapNotesGathererPluginTooltipItemTexture"..itemlinecount):Show();
								getglobal("MapNotesGathererPluginTooltipItemLeftText"..itemlinecount):Show();
								getglobal("MapNotesGathererPluginTooltipItemRightText"..itemlinecount):Show();
								height = height + 14;
--~ 								tooltip:AddDoubleLine(itemLink, string.format("x%0.2f", expected))
--~ 								tooltip:AddTexture(invTexture)
							end
						end
					end
				end
			else
				
			end
		end
	end
	for i = itemlinecount+1, itemlines do
		getglobal("MapNotesGathererPluginTooltipItemTexture"..i):Hide();
		getglobal("MapNotesGathererPluginTooltipItemLeftText"..i):Hide();
		getglobal("MapNotesGathererPluginTooltipItemRightText"..i):Hide();
	end;

	MapNotesGathererPluginTooltip:SetHeight(height);
	
--[[	local zone, index = strsplit("|", id);
	index = tonumber(index);
	local note = MapNotesInstance.portals[zone][index];
--	return note[1], note[2], note[3], note[4], note[5], note[6], note[7], note[8];

	MapLibBlizzardPOITooltipIcon:SetTexture(note[4].file); 
	MapLibBlizzardPOITooltipIcon:SetTexCoord(0, 1, 0, 1);
	MapLibBlizzardPOITooltipHeader:SetText(note[1]);
	local height = 25;
	if (note[2] and note[2]~="") then
		MapLibBlizzardPOITooltipDescription:SetText(note[2]);
		MapLibBlizzardPOITooltipDescription:Show();
		height = 41;
	else
		MapLibBlizzardPOITooltipDescription:Hide();
	end;
	if (note[3] and note[3]~="") then
		MapLibBlizzardPOITooltipDescription2:SetText(note[3]);
		MapLibBlizzardPOITooltipDescription2:Show();
		height = 57;
	else
		MapLibBlizzardPOITooltipDescription2:Hide();
	end;	
	MapLibBlizzardPOITooltip:SetHeight(height);]]

	return MapNotesGathererPluginTooltip;
end;

--~ MapNotesInstance.POI_OnClick = function(poi)
--~ 	local zone, index = strsplit("|", poi.id);
--~ 	index = tonumber(index);
--~ 	MapLib.SetMapToID(MapNotesInstance.portals[zone][index][10]);
--~ end;

-- No need for a variables loaded check since MapLib is in another addon anyway.
MapLib.POI.RegisterForPOI("MNGP_TREASURE", L["Treasures"], 6, submenu.TRE, MapNotesGathererPlugin.GetNumNodes, MapNotesGathererPlugin.GetNode, MapNotesGathererPlugin.GetTooltip, false);
MapLib.POI.RegisterForPOI("MNGP_ORE", L["Ores"], 6, submenu.ORE, MapNotesGathererPlugin.GetNumNodes, MapNotesGathererPlugin.GetNode, MapNotesGathererPlugin.GetTooltip, false);
MapLib.POI.RegisterForPOI("MNGP_HERB", L["Herbs"], 6, submenu.HER, MapNotesGathererPlugin.GetNumNodes, MapNotesGathererPlugin.GetNode, MapNotesGathererPlugin.GetTooltip, false);
